<template>
	<view class="content">
		<view class="mt">
			<view class="title">
				{{ url == 'http://61.132.67.157:8287' ? '鋆鼎WMS' : '鋆鼎Test' }}
			</view>
		</view>
		<view class="login-view">
			<input
				type="text"
				placeholder="请输入账号"
				v-model="name"
				:focus="true" />
			<input
				style="margin-top: 55rpx"
				type="password"
				placeholder="请输入密码"
				v-model="password"
				@confirm="submit" />
		</view>
		<view class="login-btn">
			<button
				class="custom-style"
				@click="$u.throttle(submit, 1000)">
				登 录
			</button>
		</view>

		<!-- #ifdef APP-PLUS -->
		<uni-popup
			ref="inputDialog"
			type="dialog">
			<uni-popup-dialog
				ref="inputClose"
				mode="input"
				title="输入内容"
				placeholder="请输入下载地址"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>

		<view class="login-btn">版本号：{{ appVersion }}</view>
		<!-- #endif -->

		<!-- #ifdef APP-PLUS -->
		<view
			class="fixedUpload"
			@click="uploadPop">
			·
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
var that;
export default {
	data() {
		return {
			name: '',
			password: '',
			appVersion: 0,
		};
	},
	onLoad() {
		that = this;
		that.url = global.reqHeard;
		// #ifdef APP-PLUS
		that.appVersion = plus.runtime.version;
		// #endif
	},

	methods: {
		setMenu() {
			var token = uni.getStorageSync('token');
			uni.request({
				url: global.reqHeard + '/api/menus/buildApp',
				header: {
					Authorization: token,
					LoginEntryType: 'mobile',
				},
				method: 'GET',
				success: (res) => {
					if (res.data.status == 401) {
						uni.reLaunch({
							url: '/pages/login/index',
						});
					} else {
						if (res.data[0].children && res.data[0].children.length > 0) {
							var list = res.data[0].children;
							var farEndList = [];
							for (var item of list) {
								farEndList.push(item.path);
							}
							uni.setStorage({
								key: 'farEndList',
								data: farEndList,
							});
						}

						uni.reLaunch({
							url: '/pages/index/index',
						});
					}
				},
				fail: (err) => {},
			});
		},
		submit() {
			if (!that.name) {
				this.$u.toast('请输入账号');
				return false;
			}
			if (!that.password) {
				this.$u.toast('请输入密码');
				return false;
			}
			uni.showLoading({
				mask: true,
				title: '登录中',
			});
			var token = uni.getStorageSync('token');
			uni.request({
				url: global.reqHeard + '/auth/appLogin',
				data: {
					username: that.name,
					password: that.password,
				},
				header: {
					Authorization: token,
					LoginEntryType: 'mobile',
				},
				method: 'POST',
				success: (res) => {
					if (res.statusCode != 200) {
						uni.showToast({
							icon: 'none',
							title: res.data.message,
						});
						return false;
					}
					var datas = res.data;
					if (datas.code != 0) {
						uni.showToast({
							icon: 'none',
							title: res.data.message,
						});
						return false;
					}
					if (!datas.data.token) {
						uni.showToast({
							icon: 'none',
							title: res.data.message,
						});
						return false;
					}
					// 本地保存token
					uni.setStorage({
						key: 'token',
						data: datas.data.token,
					});

					// name
					uni.setStorage({
						key: 'name',
						data: datas.data.user.nickName,
					});

					uni.setStorage({
						key: 'id',
						data: datas.data.user.id,
						success: function () {
							// webSocket
							const url =
								global.wsUrl + '/websocket/' + datas.data.user.id + '-MOBILE';
							console.log(url);
							uni.connectSocket({
								url,
								complete: (resWS) => {
									console.log(resWS, 'websocket');
								},
							});

							// uni.connectSocket({
							// 	url: global.wsUrl + 'websocket/' + datas.data.user.id,
							// 	header: {
							// 		'content-type': 'application/json'
							// 	},
							// 	// protocols: ['protocol1'],
							// 	method: 'GET'
							// });
							uni.hideLoading();
							// uni.reLaunch({
							// 	url: '/pages/index/index'
							// });
							that.setMenu();
						},
					});

					// uni.showToast({
					// 	icon: 'none',
					// 	title: result.data.message
					// })
					// return false
				},
				fail: (err) => {
					uni.showToast({
						icon: 'none',
						title: '登录失败',
						position: top,
					});
					return false;
				},
			});
			// this.$u
			// 	.post('/auth/appLogin', {
			// 		username: that.name,
			// 		password: that.password
			// 	})
			// 	.then(res => {});
		},
		uploadPop() {
			that.$refs.inputDialog.open();
		},
		dialogInputConfirm(val) {
			that.upLoadApp(val);
			that.$refs.inputDialog.close();
		},
		// 更新APP
		upLoadApp(downUrl) {
			let that = this;
			var num = 0;
			const downloadTask = uni.downloadFile({
				url: downUrl, //仅为示例，并非真实的资源
				success: (resFile) => {
					uni.showModal({
						title: '更新提示',
						content: '下载成功，将要安装新版本',
						showCancel: false,
						confirmText: '确定',
						success: function (res) {
							if (res.confirm == true) {
								console.log('true');
								plus.runtime.install(
									//安装
									resFile.tempFilePath,
									{
										force: true,
									},
									function (res) {
										uni.showToast({
											title: '更新成功，重启中',
											icon: 'none',
											duration: 2000,
										});
										plus.runtime.restart();
									}
								);
							}
						},
						fail: function (err) {
							console.log(err);
						},
					});
				},
			});
			downloadTask.onProgressUpdate((res) => {
				// console.log('下载进度' + res.progress);
				that.percentNum = res.progress;
			});
		},
	},
};
</script>

<style>
page {
	background-color: #fff;
}

input {
	padding: 30rpx 25rpx;
	border-radius: 10rpx;
	box-shadow: 8rpx 8rpx 15rpx 5rpx #e5e5e5;
}

.title {
	width: 100%;
	padding: 120rpx 0 100rpx 0;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 50rpx;
}

.login-view {
	width: 90%;
	margin: 0 auto;
}

.login-btn {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 55rpx;
}

.custom-style {
	width: 90%;
	color: #fff;
	background-color: #000;
	margin-top: 50rpx;
}

.fixedUpload {
	width: 80rpx;
	height: 80rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	text-align: center;
	line-height: 80rpx;
}
</style>
